package com.hiwan.unions.activity.system;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.RootActivity;
import com.hiwan.unions.adapter.bean.MembersBean;
import com.hiwan.unions.application.ConstantsValues;
import com.hiwan.unions.application.request.CallServer;
import com.hiwan.unions.application.request.ConstsNet;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.Utils;
import com.hiwan.unions.view.popupWindow.SetCheckPop;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by llg on 2017/7/8.
 */
public class MembershipActivity extends RootActivity implements HttpListener<JSONObject> {
    Request<JSONObject> request;
    private TextView text_gh, text_name, text_sex, text_mz, text_wh, text_zz, text_address, text_gz, text_zw, text_dw_address_xx, text_dw_address;
    private ArrayList<MembersBean> arry_mz = new ArrayList<MembersBean>();
    private ArrayList<MembersBean> arry_xb = new ArrayList<MembersBean>();
    private ArrayList<MembersBean> arry_wh = new ArrayList<MembersBean>();
    private ArrayList<MembersBean> arry_zz = new ArrayList<MembersBean>();
    private SetCheckPop pop;
    JSONObject jsonObject;

    public static void startPreview(Context context) {
        context.startActivity(new Intent(context, MembershipActivity.class));
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_membership;
    }

    @Override
    protected void initData() {
        stringArrayList(Utils.getJson(this, "minzu.txt"), arry_mz);
        stringArrayList(Utils.getJson(this, "sex.txt"), arry_xb);
        stringArrayList(Utils.getJson(this, "wenhua.txt"), arry_wh);
        stringArrayList(Utils.getJson(this, "zz.txt"), arry_zz);
        request = NoHttp.createJsonObjectRequest(ConstsNet.getMemberInfo, RequestMethod.POST);
        request.add("uuid", userInfo.getString(ConstantsValues.uuid, ""));
        CallServer.getRequestInstance().addToken(this, 0, request, this, true, userInfo);
    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        text_gh = findView(R.id.text_gh);
        text_name = findView(R.id.text_name);
        text_sex = findView(R.id.text_sex);
        text_mz = findView(R.id.text_mz);
        text_wh = findView(R.id.text_wh);
        text_zz = findView(R.id.text_zz);
        text_address = findView(R.id.text_address);
        text_gz = findView(R.id.text_gz);
        text_zw = findView(R.id.text_zw);
        text_dw_address = findView(R.id.text_dw_address);
        text_dw_address_xx = findView(R.id.text_dw_address_xx);
        setTitleTxt("会籍信息");
        setOperationBtn("信息变动");
    }

    @Override
    public void getOperationBtn() {
        super.getOperationBtn();
        Bundle bundle1 = new Bundle();
        bundle1.putString("json", jsonObject.toString());
        if (pop == null)
            pop = new SetCheckPop(this, bundle1);
        pop.showPopupWindow(text_name);
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        jsonObject = response.get();
        jsonObject = JsonUtil.getJSONObject(jsonObject, "data");
        text_gh.setText(JsonUtil.getString(jsonObject, "member_unionName"));
        text_name.setText(JsonUtil.getString(jsonObject, "member_name"));
        fo(arry_xb, JsonUtil.getString(jsonObject, "member_sex"), text_sex);
        fo(arry_mz, JsonUtil.getString(jsonObject, "member_nation"), text_mz);
        fo(arry_wh, JsonUtil.getString(jsonObject, "member_culture"), text_wh);
        fo(arry_zz, JsonUtil.getString(jsonObject, "member_politicalStatus"), text_zz);
        text_address.setText(JsonUtil.getString(jsonObject, "member_household_addr"));
        text_gz.setText(JsonUtil.getString(jsonObject, "member_company"));
        text_zw.setText(JsonUtil.getString(jsonObject, "member_job"));
        text_dw_address.setText(JsonUtil.getString(jsonObject, "company_city_name") + JsonUtil.getString(jsonObject, "company_district_name") + CheckUtils.check(JsonUtil.getString(jsonObject, "company_street_name"), JsonUtil.getString(jsonObject, "company_street"), "000000") + CheckUtils.check(JsonUtil.getString(jsonObject, "company_community_name"), JsonUtil.getString(jsonObject, "company_community"), "000000"));
        text_dw_address_xx.setText(JsonUtil.getString(jsonObject, "member_company_addr"));
        if (JsonUtil.getString(jsonObject, "membership_state").equals("3")) {
            getViewOperationBtn().setVisibility(View.GONE);
        }
    }

    private void stringArrayList(String s, ArrayList<MembersBean> strings) {
        try {
            JSONArray jsonArray = new JSONArray(s);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = JsonUtil.getJSONObject(jsonArray, i);
                MembersBean bean = new MembersBean();
                bean.setId(JsonUtil.getString(jsonObject, "id"));
                bean.setTitle(JsonUtil.getString(jsonObject, "title"));
                strings.add(bean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void fo(ArrayList<MembersBean> list, String title, TextView textView) {
        for (int i = 0; i < list.size(); i++) {
            MembersBean bean = list.get(i);
            if (bean.getId().equals(title)) {
                textView.setText(bean.getTitle());
                return;
            }
        }
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {

    }
}
